package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.GoodsDetailsEntity;
import com.zw.yzk.kabu.data.network.task.AddShoppingCartTask;
import com.zw.yzk.kabu.data.network.task.CollectGoodsTask;
import com.zw.yzk.kabu.data.network.task.GoodsDetailsTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.mapper.GoodsDetailsMapper;
import com.zw.yzk.kabu.view.view.GoodsDetailsView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/5.
 */

public class GoodsDetailsPresenter implements Presenter {

    private UseCase getGoodsDetails;
    private UseCase addShoppingCart;
    private UseCase collectGoods;
    private AppCompatActivity activity;
    private GoodsDetailsMapper mapper;
    private GoodsDetailsView goodsDetailsView;

    @Inject
    public GoodsDetailsPresenter(@Named("goodsDetailsTokenUseCase") UseCase getGoodsDetails,
                                 @Named("addShoppingCartUseCase") UseCase addShoppingCart,
                                 @Named("collectGoodsUseCase") UseCase collectGoods,
                                 AppCompatActivity activity, GoodsDetailsMapper mapper) {
        this.getGoodsDetails = getGoodsDetails;
        this.addShoppingCart = addShoppingCart;
        this.collectGoods = collectGoods;
        this.activity = activity;
        this.mapper = mapper;
    }

    public void setView(GoodsDetailsView view) {
        this.goodsDetailsView = view;
    }

    public void getGoodsDetails(int goodsId) {
        getGoodsDetails.execute(new GoodsDetailsSubscriber(activity), new GoodsDetailsTask(goodsId));
    }

    public void collectGoods(int goodsId, boolean collect) {
        collectGoods.execute(new CollectGoodsSubscriber(activity), new CollectGoodsTask(goodsId, collect ? 1 : 0));
    }

    public void addShoppingCart(AddShoppingCartTask.Goods goods) {
        List<AddShoppingCartTask.Goods> list = new ArrayList<>();
        list.add(goods);
        addShoppingCart.execute(new AddShoppingCartSubscriber(activity), new AddShoppingCartTask(list));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getGoodsDetails.unSubscribe();
        addShoppingCart.unSubscribe();
        collectGoods.unSubscribe();
    }

    private class CollectGoodsSubscriber extends BaseSubscriber<Boolean> {

        public CollectGoodsSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            goodsDetailsView.collectGoodsFailed();
            ToastManager.getInstance().showToast(activity, activity.getResources().getString(R.string.collected_goods_failed));
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.collected_goods_failed);
        }
    }

    private class GoodsDetailsSubscriber extends BaseSubscriber<GoodsDetailsEntity> {

        public GoodsDetailsSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_goods_details_failed);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            goodsDetailsView.getGoodsDetailFailed();
        }

        @Override
        public void onNext(GoodsDetailsEntity entity) {
            super.onNext(entity);
            goodsDetailsView.getGoodsDetailSuccess(mapper.transfer(entity));
        }
    }

    private class AddShoppingCartSubscriber extends BaseSubscriber<Boolean> {

        public AddShoppingCartSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(Boolean aBoolean) {
            super.onNext(aBoolean);
            ToastManager.getInstance().showToast(activity, activity.getResources().getString(R.string.add_shopping_cart_success));
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.add_shopping_cart_failed);
        }
    }
}
